package com.youlu.campus.admin.auth.shiro.token;

import com.alibaba.fastjson.annotation.JSONField;
import com.youlu.campus.admin.auth.common.DeviceType;
import org.apache.shiro.authc.AuthenticationToken;

import java.util.List;

/**
 * Created by zhuhuaiqi.
 *
 */
public class AuthToken implements AuthenticationToken {

    /**
     * The username
     */
    private String username;

    /**
     * The password, in char[] format
     */
    @JSONField(serialize = false)
    private char[] password;

    /**
     * Whether or not 'rememberMe' should be enabled for the corresponding login attempt;
     * default is <code>false</code>
     */
    @JSONField(serialize = false)
    private boolean rememberMe = false;

    /**
     * The location from where the login attempt occurs, or <code>null</code> if not known or
     * explicitly omitted.
     */
    @JSONField(serialize = false)
    private String host;

    /** 认证类型 */
    private AuthType authType;

    /** 域名 */
    private String domain;

    /** user_id */
    private String uid;

    /** comId */
    private String comId;

    @JSONField(serialize = false)
    private String accessToken;

    /** 是JWT的唯一标识 */
    private String jti;

    /** 最后访问时间 */
    private Long lastAccessTime;

    /** 过期时间，单位：秒 **/
    private Integer expiresIn;

    /** 角色 */
    private List<String> roles;

    @JSONField(serialize = false)
    /** 是否ttl更新 */
    private boolean updateTtl = true;

    /** 设备类型 */
    private DeviceType deviceType;

    public AuthToken() {

    }

    public AuthToken(String username, char[] password, String domain, AuthType authType) {

        this(username, password, domain, false, null, authType);
    }

    public AuthToken(String username, String password, String domain, AuthType authType) {
        this(username, password != null ? password.toCharArray() : null, domain, false, null,
                authType);
    }

    public AuthToken(String username, char[] password, String domain, boolean rememberMe,
                     AuthType authType) {
        this(username, password, domain, rememberMe, null, authType);
    }

    public AuthToken(String username, String password, String domain, boolean rememberMe,
                     AuthType authType) {

        this(username, password != null ? password.toCharArray() : null, domain, rememberMe,
                null, authType);
    }

    public AuthToken(String username, String password, String domain, boolean rememberMe,
                     String host, AuthType authType) {
        this(username, password != null ? password.toCharArray() : null, domain, rememberMe,
                host, authType);
    }

    public AuthToken(String username, String password, String domain, boolean rememberMe,
                     String host, AuthType authType, DeviceType deviceType) {
        this(username, password != null ? password.toCharArray() : null, domain, rememberMe,
                host, authType, deviceType);
    }

    public AuthToken(String username, String password, String domain, AuthType authType, boolean updateTtl) {
        this(username, password != null ? password.toCharArray() : null, domain, false, null,
                authType, updateTtl);
    }

    public AuthToken(String username, char[] password, String domain, boolean rememberMe, String
            host, AuthType authType) {
        this.username = username;
        this.password = password;
        this.domain = domain;
        this.rememberMe = rememberMe;
        this.host = host;
        this.authType = authType;
    }

    public AuthToken(String username, char[] password, String domain, boolean rememberMe, String
            host, AuthType authType, DeviceType deviceType) {
        this.username = username;
        this.password = password;
        this.domain = domain;
        this.rememberMe = rememberMe;
        this.host = host;
        this.authType = authType;
        this.deviceType = deviceType;
    }

    public AuthToken(String username, char[] password, String domain, boolean rememberMe, String
            host, AuthType authType, boolean updateTtl) {
        this.username = username;
        this.password = password;
        this.domain = domain;
        this.rememberMe = rememberMe;
        this.host = host;
        this.authType = authType;
        this.updateTtl = updateTtl;
    }

    public AuthToken(String username, char[] password, String domain, AuthType authType, DeviceType deviceType) {
        this.username = username;
        this.password = password;
        this.domain = domain;
        this.authType = authType;
        this.deviceType = deviceType;
    }

    public AuthToken(String username, String password, String domain, AuthType authType, DeviceType deviceType) {
        this(username, password != null ? password.toCharArray() : null, domain, authType, deviceType);
    }



    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public char[] getPassword() {
        return password;
    }

    public void setPassword(char[] password) {
        this.password = password;
    }

    public boolean isRememberMe() {
        return rememberMe;
    }

    public void setRememberMe(boolean rememberMe) {
        this.rememberMe = rememberMe;
    }

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public AuthType getAuthType() {
        return authType;
    }

    public void setAuthType(AuthType authType) {
        this.authType = authType;
    }

    public String getAccessToken() {
        return accessToken;
    }

    public void setAccessToken(String accessToken) {
        this.accessToken = accessToken;
    }

    public String getDomain() {
        return domain;
    }

    public void setDomain(String domain) {
        this.domain = domain;
    }

    public String getJti() {
        return jti;
    }

    public void setJti(String jti) {
        this.jti = jti;
    }

    public Long getLastAccessTime() {
        return lastAccessTime;
    }

    public void setLastAccessTime(Long lastAccessTime) {
        this.lastAccessTime = lastAccessTime;
    }

    public Integer getExpiresIn() {
        return expiresIn;
    }

    public void setExpiresIn(Integer expiresIn) {
        this.expiresIn = expiresIn;
    }

    public List<String> getRoles() {
        return roles;
    }

    public void setRoles(List<String> roles) {
        this.roles = roles;
    }

    public String getUid() {
        return uid;
    }

    public void setUid(String uid) {
        this.uid = uid;
    }

    public String getComId() {
        return comId;
    }

    public void setComId(String comId) {
        this.comId = comId;
    }

    public boolean isUpdateTtl() {
        return updateTtl;
    }

    public void setUpdateTtl(boolean updateTtl) {
        this.updateTtl = updateTtl;
    }

    public DeviceType getDeviceType() {
        return deviceType;
    }

    public void setDeviceType(DeviceType deviceType) {
        this.deviceType = deviceType;
    }

    @Override
    public Object getPrincipal() {
        return getUsername();
    }

    @Override
    public Object getCredentials() {
        return getPassword();
    }
}
